package yamen.bdwm.thread;

import java.util.List;

import yamen.bdwm.data.WmMyData;
import yamen.bdwm.data.interfaces.onListRefreshedListener;
import yamen.bdwm.datastruct.MoreAdapter;
import yamen.bdwm.datastruct.MoreAdapter.onMoreClicked;
import yamen.bdwm.datastruct.MoreAdapter.onMoreOver;
import yamen.bdwm.datastruct.WmList;
import yamen.bdwm.datastruct.WmThreadContentItem;
import android.os.AsyncTask;

public class Thread extends WmList {
	String url;
	int endnum=0;
	public Thread(String url)
	{
		this.url=url;
	}
	public void CreateAdapter(int tag,int resource)
	{
		if(!adapterList.containsKey(tag))
		{
			MoreAdapter adapter=new MoreAdapter(resource, this,new onMoreClicked() {
				
				public void Load(final onMoreOver l) {
					getMore(new onListRefreshedListener() {
						
						public void onRefreshed(int numberOfRefreshed) {
							l.OnMoreOver();
						}
						
						public void onError(int i) {
							l.OnMoreOver();
						}
					});
				}

				public boolean ShowMore() {
					// TODO Auto-generated method stub
						return true;
				}
			});
			adapterList.put(tag, adapter);
		}	
	}
	public void getMore(onListRefreshedListener onRefreshed)
	{
		getRefreshed(endnum / 50,onRefreshed);
	}
	public void getRefreshed(final int fromid,final onListRefreshedListener onRefreshed)
	{
		
		new AsyncTask<Void,Void,Integer>(){
			protected void onPostExecute(Integer i)
			{
				if(i < 49)
					((MoreAdapter)adapterList.get(0)).HideMore();
				else
					((MoreAdapter)adapterList.get(0)).ShowMore();
				
				NotifyAllAdapter();
				if(onRefreshed!=null)
					onRefreshed.onRefreshed(i);
			}
			@Override
			protected Integer doInBackground(Void... params) {
				List<WmThreadContentItem> l=WmMyData.getinstance().parser.getThreadContent(url,fromid);
				Integer j =new Integer(0);
				if(fromid==0){Clear();endnum=0;}
				for(int i=0;i<l.size();i++)
				{
					Add(l.get(i));
					j++;
					endnum ++;
				}
				return j;
			}}.execute();
	}
}
